package com.lmscn.lms.api.util;

import java.io.ByteArrayInputStream;
import java.io.File;

import com.aliyun.oss.OSSClient;
import com.lmscn.lms.api.model.Student;

/**
 * 阿里云OSS
 * 
 * 使用bucket：sxln
 * 
 * @author zhang.xx
 * @date 2016年11月21日18:05:21
 */
public class AliOssUtil {

	// 配置信息
	private static String endpoint = "oss-cn-beijing.aliyuncs.com";
	private static String accessKeyId = "2FbbIzrgBVuBCNW9";
	private static String accessKeySecret = "dsuqgBQKe0lhCgHwjma9vaS2rThA6v";

	// 静态内部类单例模式
	// 但是，这个有什么用？？？
	private AliOssUtil() {
	}

	public static AliOssUtil getInstance() {
		return AliOssUtilHolder.sInstance;
	}

	private static class AliOssUtilHolder {
		private static final AliOssUtil sInstance = new AliOssUtil();
	}

	/**
	 * 上传头像到阿里云
	 * 
	 * @date 2016年11月22日11:09:20
	 * @doc 
	 *      https://help.aliyun.com/document_detail/32013.html?spm=5176.doc32015.6
	 *      .216.1hF85o
	 */
	public static void uploadHeadimgToOss(byte[] content, Student student) {
		try {
			OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

			ossClient.putObject("sxln", "head_imgs/" + student.getLong("user_id") + ".jpg", new ByteArrayInputStream(content));

			ossClient.shutdown();

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

	}

	/**
	 * 测试方法
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
	}
}
